﻿using Microsoft.Win32;
using System;
using System.IO;
using System.Text;
using System.Windows;
using WpfClient.FunctionFileParsing;

namespace WpfClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Parse_Click(object sender, RoutedEventArgs e)
        {
            FunctionsFileParser parser = new FunctionsFileParser();
            parser.Parse(this.textboxToParse.Text);

            this.Show(parser.Result);
        }

        private void ChooseFilesAndWriteDocumentation_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;            

            if (openFileDialog.ShowDialog() == true)
            {
                StringBuilder builder = new StringBuilder();

                FunctionsFileParser parser = new FunctionsFileParser();

                foreach (string fileName in openFileDialog.FileNames)
                {
                    using (StreamReader reader = new StreamReader(fileName, Encoding.UTF8))
                    {
                        string content = reader.ReadToEnd();
                        parser.Parse(content);

                        builder.AppendFormat(@"-------------------------------
{0}
-----------------------------------------
", parser.Result);
                    }

                    using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.UTF8))
                    {
                        writer.Write(parser.Result);
                    }
                }

                this.Show(builder.ToString());
            }
        }

        private void Show(string text)
        {
            this.parsedResult.Text = text;
        }
    }
}
